<!DOCTYPE html>
<html>

<head>
    <title>LoadVirtualOptionsSolution Demo</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui/css/layui.css">
    <style>
        .select-container {
            margin: 20px;
            display: flex;
            align-items: center;
            gap: 20px;
        }
    </style>
</head>

<body class="layui-form">
    <div class="select-container">
        <label>省份选择：</label>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 300px;">
                <select name="provinceSelect" id="provinceSelect" lay-filter="provinceSelect" lay-search lay-creatable lay-verify="required">
                </select>
            </div>
        </div>
    </div>

    <div class="select-container">
        <label>城市选择：</label>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 300px;">
                <select name="citySelect" id="citySelect" lay-filter="citySelect" lay-search lay-verify="required">
                </select>
            </div>
        </div>
    </div>

    <div class="select-container">
        <label>区域选择：</label>
        <div class="layui-inline">
            <div class="layui-input-inline" style="width: 300px;">
                <select name="areaSelect" id="areaSelect" lay-filter="areaSelect" lay-search lay-verify="required">
                </select>
            </div>
        </div>
    </div>

    <script src="https://www.layuicdn.com/layui/layui.js"></script>
    <script src="loadVirtualOption.js"></script>
    <script>
        layui.use(['form', 'layer'], function () {
            const form = layui.form;
            const layer = layui.layer;
            const $ = layui.$;
            // 生成测试数据
            function generateMockData(prefix, count) {
                return Array.from({ length: count }, (_, i) => ({
                    value: i,
                    text: `${prefix}-${i}`
                }));
            }
            // 创建虚拟选择器实例，传入 layui 依赖
            const virtualSelect = new LoadVirtualOptionsSolution({
                selectName: {
                    'provinceSelect': '省份',
                    'citySelect': '城市',
                    'areaSelect': '区域'
                },
                form: form,
                layer: layer,
                $: $
            });

            // 直接使用实例方法，无需等待初始化
            const dataMap = {
                'provinceSelect': generateMockData('省份', 5000),
                'citySelect': generateMockData('城市', 8000),
                'areaSelect': generateMockData('区域', 10000)
            };

            virtualSelect.loadBatchData(dataMap);

            // 使用 form.on 监听选择事件
            form.on('select(provinceSelect)', function(data) {
                layer.msg('选择的省份：' + data.value);
            });

            form.on('select(citySelect)', function(data) {
                layer.msg('选择的城市：' + data.value);
            });

            form.on('select(areaSelect)', function(data) {
                layer.msg('选择的区域：' + data.value);
            });
        });
    </script>
    <script src="performance-monitor.js"></script>
    <script>
        const monitor = new PerformanceMonitor({
            warningThreshold: 30,
            showPanel: true,
            historySize: 10,
            updateInterval: 500,
            solutionName: 'LoadVirtualOptionsSolution方案',
            onFPSUpdate: ({ fps, avgFps }) => {
                if (fps < 30) {
                    console.warn(`[LoadVirtualOptionsSolution方案][${new Date().toLocaleTimeString()}] 性能警告: FPS=${fps}`);
                }
            }
        });

        document.addEventListener('DOMContentLoaded', () => {
            monitor.start();
        });
    </script>
</body>

</html>